package com.gxa.zhonglong.controller;

import com.gxa.zhonglong.common.JsonResult;
import com.gxa.zhonglong.exception.BusinessException;
import com.gxa.zhonglong.service.RoleMenuService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author ws
 * @date 2021/10/21 10:38
 */
@Api(tags = "角色权限操作接口api")
@CrossOrigin
@RestController
@RequestMapping("/roleMenu")
public class RoleMenuController {

    @Autowired
    private RoleMenuService roleMenuService;


    @PostMapping("/update")
    @ApiOperation("通过id修改角色权限的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "map", value = "json字符串，包含roleId和数组menuIds(eg:{\"roleId\":\"5\",\"menuIds\":[5,6]})", required = true, dataType = "object", paramType = "body"),
    })
    public Map<String, Object> update(@RequestBody Map map) throws BusinessException {
        Long roleId = null;
        List menuIds = null;
        System.out.println(map);
        if (map!=null) {
            roleId = Long.parseLong(map.get("roleId").toString());
            menuIds = (List) map.get("menuIds");
            System.out.println(roleId);
            System.out.println(menuIds);
        }
        roleMenuService.update(roleId, menuIds);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        return jsonResult.getDataMap();
    }

    @PostMapping("/getList")
    @ApiOperation("通过id查询角色权限的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色编号", required = true, dataType = "Long", paramType = "query")
    })
    public Map<String, Object> getList(@ApiParam("id") Long id) {
        List<List<Object>> list = roleMenuService.getList(id);
        List<Map<String, Object>> menuList = roleMenuService.getMenuList();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(list);
        jsonResult.put("menuList", menuList);
        return jsonResult.getDataMap();
    }


}
